﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using SongSharp.Appliance;
using SongSharp.Presentation.View;
using SongSharp.Presentation.Command;

namespace SongSharp.Presentation.ViewModel
{
    public class ThinkViewModel : ViewModelBase
    {
        private ThinkView ThinkView { get; set; }
        private BackgroundWorker Worker { get; set; }
        private PrimeViewModel PrimeModel { get; set; }
        private Window Owner { get; set; }

        public ThinkViewModel(PrimeViewModel primeModel, Window owner)
        {
            PrimeModel = primeModel;
            Owner = owner;
        }

        public void ShowWindow()
        {
            if (ThinkView == null || !ThinkView.IsLoaded)
            {
                ThinkView = new ThinkView();
                ThinkView.DataContext = this;
                ThinkView.Owner = Owner;
                ThinkView.Show();
            }
            else
            {
                ThinkView.Activate();
            }

            Worker = new BackgroundWorker();
            Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

            Worker.RunWorkerAsync();
        }

        void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PrimeModel.MusicStream_created(sender, e);
            ThinkView.Close();
        }

        void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            QueueApp.Clear();
            QueueApp.EnqueueRange(SuggestionApp.CreateSuggestions());
        }
    }
}
